<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);

class c_ajax_class extends AWS_CONTROLLER
{

	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'white';
		
		$rule_action['actions'] = array(
			'get_question_comments', 
			'get_answer_comments', 
			'log', 
			'load_attach', 
			'get_focus_users', 
			'get_answer_users'
		);
		
		if (get_setting('guest_explorer') == 'Y')
		{
			$rule_action['actions'][] = 'discuss';
		}
		
		return $rule_action;
	}

	function setup()
	{
		HTTP::no_cache_header();
	}

	public function uninterested_action()
	{
		$question_id = intval($_POST['question_id']);
		
		if ($question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
			exit();
		}
		
		$this->model('question')->add_question_uninterested($this->user_id, $question_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '操作成功'));
	}

	public function get_focus_users_action()
	{
		$focus_users = $this->model('question')->get_focus_users_by_question($_GET['question_id']);
		
		if (is_array($focus_users))
		{
			foreach ($focus_users as $key => $val)
			{
				$focus_users[$key]['avatar_file'] = get_avatar_url($val['uid'], 'mid', $val['avatar_file']);
			}
		}
		
		H::ajax_json_output($focus_users);
	}

	public function get_answer_users_action()
	{
		H::ajax_json_output($this->model('answer')->get_answer_users_by_question($_GET['question_id']));
	}

	public function add_invite_action()
	{
		$question_id = intval($_GET['question_id']);
		$recipients_uid = intval($_GET['uid']);
		
		if ($question_id == 0 || $recipients_uid == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题或用户不存在"));
		}
		
		if ($recipients_uid == $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "不能邀请自己回复问题"));
		}
		
		$question_info = $this->model('question')->get_question_info_by_id($question_id);
		
		/*if ($question_info['published_uid'] == $recipients_uid)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "不能邀请问题发布者"));
		}*/
		
		if ($this->model('question')->check_question_invite($question_id, 0, $recipients_uid))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "该用户已接受过邀请。"));
		}
		
		if ($this->model('question')->check_question_invite($question_id, $this->user_id, $recipients_uid))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "已邀请过该用户。"));
		}
		
		if ($this->model('question')->add_invite($question_id, $this->user_id, $recipients_uid))
		{
			$this->model('notify')->send($this->user_id, $recipients_uid, notify_class::TYPE_INVITE_QUESTION, notify_class::CATEGORY_QUESTION, $question_id, array(
				'from_uid' => $this->user_id, 
				'question_id' => $question_id
			));
			
			$this->model('email')->action_email(email_class::TYPE_QUESTION_INVITE, $recipients_uid, get_js_url('/question/' . $question_id), array(
				'user_name' => $this->user_info['user_name'], 
				'question_title' => $question_info['question_content']
			));
			
			H::ajax_json_output(AWS_APP::RSM(null, "1", "邀请用户成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(l, "-1", "邀请失败, 数据库无法写入。"));
		}
	}

	public function update_answer_action()
	{
		$answer_id = intval($_GET['answer_id']);
		$question_id = intval($_GET['question_id']);
		$answer_content = $_POST['answer_content'];
		$answer_content = trim($answer_content, "\r\n\t");
		
		if (! $answer_info = $this->model('answer')->get_answer_info_by_id($answer_id))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'answer_content'
			), "-2", "回复不存在"));
		}
		
		if (empty($answer_content))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'answer_content'
			), "-2", "请输入回复内容"));
		}
		
		if (cjk_strlen($answer_content) < get_setting('answer_length_lower'))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "回复内容字数不得少于" . get_setting('answer_length_lower') . '个，请返回修改。'));
		}
		
		if (! $this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($answer_content))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你所在的用户组不允许发布站外链接"));
		}
		
		if ($answer_info['uid'] != $this->user_id and ! $this->user_info['permission']['is_administortar'] and ! $this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你没有权限编辑这个回复"));
		}
		
		if ($answer_info['uid'] == $this->user_id and (time() - $answer_info['add_time'] > get_setting('answer_edit_time') * 60))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", '发布时间超过 ' . get_setting('answer_edit_time') . ' 分钟的回复不允许编辑'));
		}
		
		$this->model('answer')->update_answer($answer_id, $question_id, $answer_content, 0, $_POST['attach_access_key']);
		
		//$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::MOD_ANSWER);
		

		ZCACHE::cleanGroup("question_detail_" . $answer_info['question_id']);
		
		$answer_content = FORMAT::parse_links($answer_content);
		
		//$this->model('associate_index')->update_update_time($question_id, 1);
		//$this->model('associate_index')->update_update_time($question_id, 3);
		

		H::ajax_json_output(AWS_APP::RSM(array(
			'target_id' => $_GET['target_id'], 
			'display_id' => $_GET['display_id']
		), "1", nl2br($answer_content)));
	}

	public function edit_answer_action()
	{
		$answer_id = intval($_GET['answer_id']);
		
		if (! $answer_info = $this->model('answer')->get_answer_info_by_id($answer_id))
		{
			H::redirect_msg('回复不存在');
		}
		
		if ($answer_info['uid'] != $this->user_id and ! $this->user_info['permission']['is_administortar'] and ! $this->user_info['permission']['is_moderator'])
		{
			H::redirect_msg('你没有权限编辑这个回复');
		}
		
		if ($answer_info['uid'] == $this->user_id and (time() - $answer_info['add_time'] > get_setting('answer_edit_time') * 60))
		{
			H::redirect_msg('发布时间超过 ' . get_setting('answer_edit_time') . ' 分钟的回复不允许编辑');
		}
		
		TPL::assign('answer_info', $answer_info);
		TPL::assign('attach_access_key', md5($this->user_id . time()));
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('question/ajax/edit_answer');
	}

	function agree_answer_action()
	{
		$retval = $this->model('answer')->agree_answer($this->user_id, $_POST['answer_id']);
		
		$answer_info = $this->model('answer')->get_answer_info_by_id($_POST['answer_id']);
		
		ZCACHE::cleanGroup('question_detail_' . $answer_info['question_id']);
		
		if ($retval)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'agree'
			)), "1", "赞同发送成功");
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'disagree'
			)), "1", "赞同发送成功");
		}
	}

	public function question_share_txt_action()
	{
		$question_id = intval($_GET['question_id']);
		
		$q_info = $this->model('question')->get_question_info_by_id($question_id);
		
		$q_info['question_content'] = cjk_substr($q_info['question_content'], 0, 100, 'UTF-8', '...');
		
		$url = get_js_url("/question/" . $question_id . "?fromuid-" . $this->user_id);
		
		$email_message = (array)AWS_APP::config()->get('email_message');
		
		foreach ($email_message[email_class::TYPE_QUESTION_SHARE] as $key => $val)
		{
			$$key = str_replace('[#user_name#]', $this->user_info['user_name'], $val);
			$$key = str_replace('[#site_name#]', get_setting('site_name'), $$key);
			$$key = str_replace('[#question_title#]', $q_info['question_content'], $$key);
		}
		
		$data = array(
			'weibo' => "#" . get_setting('site_name') . "# " . $q_info['question_content'] . $url . " (分享自@" . get_setting('site_name') . ")", 
			'mail' => $message, 
			'message' => "我看到一个不错的问题，想和你分享：“" . $q_info['question_content'] . "” " . $url
		);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'share_txt' => $data
		), 1));
	}

	public function answer_share_txt_action()
	{
		$answer_id = intval($_GET['answer_id']);
		
		$a_info = $this->model('answer')->get_answer_info_by_id($answer_id);
		
		$u_info = $this->model('account')->get_users_by_uid($a_info['uid']);
		
		$q_info = $this->model('question')->get_question_info_by_id($a_info['question_id']);
		
		$a_info['answer_content'] = trim(cjk_substr($a_info['answer_content'], 0, 100, 'UTF-8', '...'));
		
		$url = get_js_url("/question/" . $a_info['question_id'] . "?item_id-" . $answer_id . "__fromuid-" . $this->user_id . '#answer_' . $answer_id);
		
		$email_message = (array)AWS_APP::config()->get('email_message');
		
		foreach ($email_message[email_class::TYPE_ANSWER_SHARE] as $key => $val)
		{
			$$key = str_replace('[#user_name#]', $this->user_info['user_name'], $val);
			$$key = str_replace('[#site_name#]', get_setting('site_name'), $$key);
			$$key = str_replace('[#question_title#]', $q_info['question_content'], $$key);
			$$key = str_replace('[#answer_user#]', $u_info['user_name'], $$key);
			$$key = str_replace('[#answer_content#]', cjk_substr($a_info['answer_content'], 0, 300, 'UTF-8', '...'), $$key);
		}
		
		$data = array(
			'weibo' => cjk_substr("#" . get_setting('site_name') . "# " . $q_info['question_content'] . " " . '@' . $u_info['user_name'] . "： " . $a_info['answer_content'], 0, 100, 'UTF-8', '...') . ' ' . $url . " (分享自@" . get_setting('site_name') . ")", 
			'mail' => $message, 
			'message' => "我看到一个不错的问题，想和你分享：“" . $q_info['question_content'] . "” " . $u_info['user_name'] . "： " . cjk_substr($a_info['answer_content'], 0, 300, 'UTF-8', '...') . ' ' . $url
		);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'share_txt' => $data
		), 1));
	}

	public function send_share_email_action()
	{
		if (! $_POST['email_message'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '请输入邮件内容'));
		}
		
		if (! H::valid_email($_POST['email_address']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '邮件地址格式错误'));
		}
		
		if ($_POST['email_address'] == $this->user_info['email'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'share_send'
			), '-1', '不能发送给自己'));
		}
		
		$email_hash = base64_encode(H::encode_hash(array(
			'email' => $_POST['email_address']
		)));
		
		switch ($_POST['model_type'])
		{
			case 'answer' :
				$action = email_class::TYPE_ANSWER_SHARE;
				$a_info = $this->model('answer')->get_answer_by_id($_POST['target_id']);
				$q_info = $this->model('question')->get_question_info_by_id($a_info['question_id']);
				$link_title = $q_info['question_content'];
				$url = get_js_url('/question/' . $a_info['question_id'] . '?fromuid-' . $this->user_id . '__item_id-' . $_POST['target_id'] . '__fromemail-' . $email_hash . '#answer_' . $_POST['target_id']);
				break;
				
			case 'question' :
				$action = email_class::TYPE_QUESTION_SHARE;
				$q_info = $this->model('question')->get_question_info_by_id($_POST['target_id']);
				$link_title = $q_info['question_content'];
				$url = get_js_url('/question/' . $q_info['question_id'] . '?fromuid-' . $this->user_id . '__fromemail-' . $email_hash);
				break;
				
			case 'topic' :
				$action = email_class::TYPE_TOPIC_SHARE;
				$t_info = $this->model('topic')->get_topic($_POST['target_id']);
				$link_title = $t_info['topic_title'];
				$url = get_js_url('/topic/detail/' . $t_info['topic_id'] . '?fromuid-' . $this->user_id . '__fromemail-' . $email_hash);
				break;
				
			default :
		}
		
		$email_message = (array)AWS_APP::config()->get('email_message');
		
		foreach ($email_message[$action] as $key => $val)
		{
			$$key = str_replace('[#user_name#]', $this->user_info['user_name'], $val);
			$$key = str_replace('[#site_name#]', get_setting('site_name'), $$key);
			$$key = str_replace('[#question_title#]', $q_info['question_content'], $$key);
			$$key = str_replace('[#topic_title#]', $t_info['topic_title'], $$key);
		}
		
		$this->model('email')->send($_POST['email_address'], $subject, nl2br($_POST['email_message']), $url, $link_title);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '分享发送成功'));
	}

	function discuss_action()
	{
		if ($_GET['sort_type'] == 'unresponsive')
		{
			$_GET['answer_count'] = '0';
		}
		
		$question_list = $this->model('question')->get_discuss_list($_GET['page'], get_setting('contents_per_page'), $_GET['sort_type'], $_GET['topic_id'], $this->user_id, $_GET['category'], $_GET['answer_count'], $_GET['day']);
		
		TPL::assign('question_list', $question_list);
		
		if ($_GET['template'] == 'topic')
		{
			TPL::output("question/ajax/list_topic");
		}
		else
		{
			TPL::output("question/ajax/list");
		}
	}

	public function save_answer_comment_action()
	{
		if (! $_GET['answer_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "回复不存在"));
		}
		
		if (trim($_POST['message']) == '')
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "请输入评论内容"));
		}
		
		if ((get_setting('comment_limit') > 0) && (cjk_strlen($_POST['message']) > get_setting('comment_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "评论内容字数不得超过" . get_setting('comment_limit') . "个"));
		}
		
		if (! $this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($_POST['message']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你所在的用户组不允许发布站外链接"));
		}
		
		$message = $this->model('question')->parse_at_user($_POST['message'], false, false, true);
		
		if ($comment_id = $this->model('answer')->insert_answer_comment($_GET['answer_id'], $this->user_id, $message))
		{
			$answer = $this->model('answer')->get_answer_info_by_id($_GET['answer_id']);
			
			$this->model('notify')->send($this->user_id, $answer['uid'], notify_class::TYPE_ANSWER_COMMENT, notify_class::CATEGORY_QUESTION, $answer['question_id'], array(
				'comment_type' => 2, 
				'from_uid' => $this->user_id, 
				'question_id' => $answer['question_id'], 
				'item_id' => $_GET['answer_id'], 
				'comment_id' => $comment_id
			));
			
			$message_arr = $this->model('question')->parse_at_user($message, false, true);
			
			if ($message_arr['user'])
			{
				foreach ($message_arr['user'] as $uid)
				{
					if ($uid == $answer['uid'])
					{
						continue;
					}
					
					$this->model('notify')->send($this->user_id, $uid, notify_class::TYPE_COMMENT_AT_ME, notify_class::CATEGORY_QUESTION, $answer['question_id'], array(
						'comment_type' => 2, 
						'from_uid' => $this->user_id, 
						'question_id' => $answer['question_id'], 
						'item_id' => $_GET['answer_id'], 
						'comment_id' => $comment_id
					));
				}
			}
		}
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'item_id' => $_GET['answer_id'], 
			'type_name' => 'answer'
		), 1, "评论成功"));
	}

	public function get_answer_comments_action()
	{
		$comments = $this->model('answer')->get_answer_comments($_GET['answer_id']);
		
		foreach ($comments as $key => $val)
		{
			$comments[$key]['message'] = FORMAT::parse_links($this->model('question')->parse_at_user($comments[$key]['message']));
			$user_info = $this->model('account')->get_users_by_uid($val['uid']);
			$comments[$key]['user_name'] = $user_info['user_name'];
		}
		
		TPL::assign('comments', $comments);
		
		TPL::output("question/ajax/comments");
	}

	public function save_question_comment_action()
	{
		if (! $_GET['question_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "问题不存在"));
		}
		
		if (trim($_POST['message']) == '')
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "请输入评论内容"));
		}
		
		if ((get_setting('comment_limit') > 0) && (cjk_strlen($_POST['message']) > get_setting('comment_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "评论内容字数不得超过" . get_setting('comment_limit') . "个"));
		}
		
		$message = $this->model('question')->parse_at_user($_POST['message'], false, false, true);
		
		$comment_id = $this->model('question')->insert_question_comment($_GET['question_id'], $this->user_id, $message);
		
		if ($comment_id)
		{
			$question = $this->model('question')->get_question_info_by_id($_GET['question_id']);
			
			$this->model('notify')->send($this->user_id, $question['published_uid'], notify_class::TYPE_QUESTION_COMMENT, notify_class::CATEGORY_QUESTION, $question['question_id'], array(
				'comment_type' => 1, 
				'from_uid' => $this->user_id, 
				'question_id' => $question['question_id'], 
				'comment_id' => $comment_id
			));
			
			$message_arr = $this->model('question')->parse_at_user($message, false, true);
			
			if ($message_arr['user'])
			{
				foreach ($message_arr['user'] as $uid)
				{
					if ($uid == $question['published_uid'])
					{
						continue;
					}
					
					$this->model('notify')->send($this->user_id, $uid, notify_class::TYPE_COMMENT_AT_ME, notify_class::CATEGORY_QUESTION, $question['question_id'], array(
						'comment_type' => 1, 
						'from_uid' => $this->user_id, 
						'question_id' => $question['question_id'], 
						'comment_id' => $comment_id
					));
				}
			}
		}
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'item_id' => $_GET['question_id'], 
			'type_name' => 'question'
		), 1, "评论成功"));
	}

	public function get_question_comments_action()
	{
		if ($comments = $this->model('question')->get_question_comments($_GET['question_id']))
		{
			foreach ($comments as $key => $val)
			{
				$comments[$key]['message'] = FORMAT::parse_links($this->model('question')->parse_at_user($comments[$key]['message']));
				$user_info = $this->model('account')->get_users_by_uid($val['uid']);
				$comments[$key]['user_name'] = $user_info['user_name'];
			}
		}
		
		TPL::assign('comments', $comments);
		
		TPL::output("question/ajax/comments");
	}

	public function change_vote_action()
	{
		$answer_id = intval($_POST['answer_id']);
		$value = intval($_POST['value']);
		
		$answer_info = $this->model('answer')->get_answer_by_id($answer_id);
		
		if ($answer_info['uid'] == $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "不能对自己发表的回复投票"));
		}
		
		if (! in_array($value, array(
			- 1, 
			1
		)))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "投票值错误，无法投票。"));
		}
		
		$reputation_factor = $this->model('account')->get_group_by_id($this->user_info['reputation_group'], 'reputation_factor');
		
		//p($reputation_factor);
		

		$retval = $this->model('answer')->change_answer_vote($answer_id, $value, $this->user_id, $reputation_factor);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, null));
	}

	public function cancel_question_invite_action()
	{
		$question_id = intval($_GET['question_id']);
		$recipients_uid = intval($_GET['recipients_uid']);
		
		$this->model('question')->cancel_question_invite($question_id, $this->user_id, $recipients_uid);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, "取消邀请成功"));
	}

	public function question_invite_delete_action()
	{
		$question_invite_id = intval($_POST['question_invite_id']);
		
		$this->model('question')->delete_question_invite($question_invite_id, $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, "删除邀请成功"));
	}

	public function question_answer_rate_action()
	{
		$answer_info = $this->model('answer')->get_answer_by_id($_POST['answer_id']);
		
		if ($this->user_id == $answer_info['uid'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '不能评价自己发表的回复'));
		}
		
		if ($_POST['type'] == 'thanks' && $this->model("answer")->user_rated('thanks', $_POST['answer_id'], $this->user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '已感谢过该回复，请不要重复感谢。'));
		}
		
		if ($this->model('answer')->user_rate($_POST['type'], $_POST['answer_id'], $this->user_id, $this->user_info['user_name']))
		{
			//通知回复作者
			$data = array(
				'question_id' => $answer_info['question_id'], 
				'from_uid' => $this->user_id, 
				'item_id' => $answer_info['answer_id']
			);
			
			$this->model('notify')->send($this->user_id, $answer_info['uid'], notify_class::TYPE_ANSWER_THANK, notify_class::CATEGORY_QUESTION, $answer_info['question_id'], $data);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'add'
			), 1, '回复评价成功'));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'remove'
			), 1, '撤消评价成功'));
		}
	}

	public function focus_action()
	{
		$question_id = intval($_GET['question_id']);
		
		if ($question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
		}
		
		if (! $this->model('question')->get_question_info_by_id($question_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
		}
		
		$retval = $this->model('question')->add_focus_question($question_id);
		
		if ($retval)
		{
			ZCACHE::delete("question_focus_q" . $question_id . "_u" . $this->user_id);
			
			ZCACHE::delete("question_get_user_recommend_v2_" . $this->user_id . "_10");
			
			H::ajax_json_output(AWS_APP::RSM(array(
				"type" => $retval
			), "1", "关注成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				"type" => $retval
			), "-1", "关注失败, 请联系管理员"));
		}
	}

	/**
	 *
	 * 添加回复
	 *
	 * @return boolean true|false
	 */
	public function save_answer_action()
	{
		$question_id = intval($_POST['question_id']);
		$answer_content = $_POST['answer_content'];
		$answer_content = trim($answer_content, "\r\n\t");
		
		if (! $question_id)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'sn'
			), "-1", '问题不存在'));
		}
		
		if (! $answer_content)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请输入回复内容"));
		}
		
		//判断是否是问题发起者
		if (get_setting('answer_self_question') == 'N')
		{
			$question_info = $this->model('question')->get_question_info_by_id($_POST['question_id']);
			
			if ($question_info['published_uid'] == $this->user_id)
			{
				H::ajax_json_output(AWS_APP::RSM(null, "-1", "不能回复自己发布的问题，你可以在问题发布后的 " . get_setting('question_edit_time') . " 分钟内修改问题"));
			}
		}
		
		//判断是否已回复过问题
		if ((get_setting('answer_unique') == 'Y') && $this->model('answer')->check_answer_question($_POST['question_id'], $this->user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "一个问题只能回复一次，你可以编辑回复过的回复"));
		}
		
		if (strlen($answer_content) < get_setting('answer_length_lower'))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "回复内容字数不得少于 " . get_setting('answer_length_lower') . ' 字节，请返回修改。'));
		}
		
		if (! $this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($answer_content))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你所在的用户组不允许发布站外链接"));
		}
		
		if (human_valid('answer_valid_hour') and ! core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请填写正确的验证码"));
		}
		
		// !注: 来路检测后面不能再放报错提示
		if (! valid_post_hash($_POST['post_hash']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", '表单来路不正确或内容已提交, 请刷新页面重试'));
		}
		
		core_captcha::clear();
		
		if ($answer_id = $this->model('answer')->save_answer($question_id, $answer_content))
		{
			set_human_valid('answer_valid_hour');
			
			$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ANSWER_QUESTION);
			
			ZCACHE::cleanGroup("question_detail_" . $question_id);
			
			if (! $this->model('question')->has_focus_question($question_id, $this->user_id))
			{
				$this->model('question')->add_focus_question($question_id);
				$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ADD_REQUESTION_FOCUS);
			}
			
			$question_info = $this->model('question')->get_question_info_by_id($question_id);
			
			$notify_uids = array();
			
			if ($focus_uids = $this->model('question')->get_focus_uid_by_question_id($question_id))
			{
				foreach ($focus_uids as $focus_user)
				{
					$this->model('email')->action_email(email_class::TYPE_NEW_ANSWER, $focus_user['uid'], get_js_url('/question/' . $question_id), array(
						'question_title' => $question_info['question_content']
					));
					
					$notify_uids[] = $focus_user['uid'];
					
					$data = array(
						'question_id' => $question_id, 
						'from_uid' => $this->user_id, 
						'item_id' => $answer_id
					);
					
					$this->model('notify')->send($this->user_id, $focus_user['uid'], notify_class::TYPE_NEW_ANSWER, notify_class::CATEGORY_QUESTION, $question_id, $data);
				}
			}
			
			//删除回复邀请
			$this->model('question')->answer_question_invite($question_id, $this->user_id);
			
			if ($_POST['attach_access_key'])
			{
				$this->model('publish')->update_attach('answer', $answer_id, $_POST['attach_access_key']);
			}
			
			$this->model('draft')->delete_draft($question_id, 'answer', $this->user_id);
			$this->model('question')->delete_question_uninterested($this->user_id, $question_id);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'question_id' => $question_id, 
				'action_type' => $answer_id, 
				'url' => get_js_url('/question/id-' . $question_id . '__item_id-' . $answer_id . '__rf-false')
			), 1, "问题回复成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "回复问题失败, 请联系管理员"));
		}
	}

	/**
	 * 
	 * 根据话题ID和问题ID，删除关联
	 * 
	 * @return boolean true|false
	 */
	public function delete_topic_action()
	{
		// 		if (!$this->user_info['permission']['edit_question'])
		// 		{
		// 			H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限进行操作'));
		// 			exit();
		// 		}
		

		$topic_id = intval($_GET['topic_id']);
		$question_id = intval($_GET['question_id']);
		
		if ($topic_id == 0 || $question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
		}
		
		if ($this->model('topic')->delete_topic($topic_id, $question_id, 0))
		{
			ZCACHE::delete("question_topics_" . $question_id);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'topic_id' => $topic_id
			), 1, "删除话题成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "删除话题失败, 请联系管理员"));
		}
	}

	/**
	 * 
	 * 根据问题ID增加话题，并直接关联
	 * 
	 * @return boolean true|false
	 */
	public function save_topic_action()
	{
		$retval = false;
		$topic_title = trim(htmlspecialchars($_POST['topic_title']));
		$question_id = intval($_GET['question_id']);
		$topic_lock = intval($_GET['topic_lock']);
		
		if ($question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'sn'
			), "-1", "问题不存在"));
		}
		
		if (empty($topic_title))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'content'
			), "-1", "请输入话题标题"));
		}
		
		if (preg_match('/\//i', $topic_title))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'content'
			), "-1", '话题不能包含"/"'));
		}
		
		if (! $this->model('topic')->get_topic_id_by_title($topic_title) && (get_setting('topic_title_limit') > 0) && (cjk_strlen($topic_title) > get_setting('topic_title_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '话题标题字数不得超过' . get_setting('topic_title_limit') . '个'));
		}
		
		if ($question_id > 0)
		{
			$topic_id = $this->model('topic')->save_topic($question_id, $topic_title, $this->user_id, 0, '', '', $topic_lock, 1);
			$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ADD_TOPIC, $this->user_id);
		}
		
		if (! $topic_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "话题已锁定, 不能添加话题"));
			exit();
		}
		
		if ($this->model('question_topic')->save_link($topic_id, $question_id))
		{
			ZCACHE::delete("question_topics_" . $question_id);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'topic_id' => $topic_id
			), 1, "添加话题成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "添加话题失败, 请联系管理员"));
		}
	}

	function remove_answer()
	{
		$answer_info = $this->model('answer')->get_answer_by_id(intval($_POST['answer_id']));
		
		if (! $answer_info)
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "回复不存在"));
		}
		
		if ($answer_info['uid'] != $this->user_id and ! $this->user_info['permission']['is_administortar'] and ! $this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "对不起，你没有删除权限。"));
		}
		
		if ($this->model('answer')->remove_answers_by_ids(intval($_POST['answer_id'])))
		{
			//通知回复的作者
			if ($this->user_id != $answer_info['uid'])
			{
				$this->model('notify')->send($this->user_id, $answer_info['uid'], notify_class::TYPE_REMOVE_ANSWER, notify_class::CATEGORY_QUESTION, $answer_info['question_id'], array(
					'from_uid' => $this->user_id, 
					'question_id' => $answer_info['question_id']
				));
			}
			
			H::ajax_json_output(AWS_APP::RSM(null, 1, "删除问题成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "删除失败，无法操作数据库"));
		}
	}

	function log_action()
	{
		$question_id = $_GET['id'];
		
		$log_list = ACTION_LOG::get_action_by_event_id(false, $question_id, calc_page_limit($_GET['page'], 10), ACTION_LOG::CATEGORY_QUESTION, '101, 102, 103, 401, 405, 107, 108, 109, 110');
		
		//处理日志记录
		

		$log_list = $this->model('question')->analysis_log($log_list);
		
		$question_info = $this->model('question')->get_question_info_by_id($question_id);
		
		//if ($question_info['published_uid'] == $this->user_id OR $this->user_info['permission']['is_administortar'] OR $this->user_info['permission']['is_moderator'])
		//{
		if (! $unverified_modify_all = $question_info['unverified_modify'])
		{
			$unverified_modify_all = array();
		}
		
		$unverified_modify = array();
		
		foreach ($unverified_modify_all as $key => $val)
		{
			$unverified_modify = array_merge($unverified_modify, $val);
		}
		
		TPL::assign('unverified_modify', $unverified_modify);
		TPL::assign('question_info', $question_info);
		//}
		

		TPL::assign('list', $log_list);
		
		TPL::output('question/ajax/log');
	}

	function redirect_action()
	{
		$this->model('question')->redirect($this->user_id, $_POST['item_id'], $_POST['target_id']);
		
		if (intval($_POST['target_id']) && intval($_POST['item_id']))
		{
			$q_info = $this->model('question')->get_question_info_by_id($_POST['item_id']);
			
			$this->model('notify')->send($this->user_id, $q_info['published_uid'], notify_class::TYPE_REDIRECT_QUESTION, notify_class::CATEGORY_QUESTION, $_POST['item_id'], array(
				'from_uid' => $this->user_id, 
				'question_id' => $_POST['item_id']
			));
		}
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, null));
	}

	function email_invite_action()
	{
		$email = $_POST['email'];
		
		if (! H::valid_email($email))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '请填写正确的Email'));
		}
		
		if ($email == $this->user_info['email'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '不能邀请自己。'));
		}
		
		if ($this->model('question')->check_email_invite($_GET['question_id'], $this->user_id, $email))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '此Email已接收过邀请。'));
		}
		
		if ($this->model('question')->add_invite($_GET['question_id'], $this->user_id, 0, $_POST['email']))
		{
			$q_info = $this->model('question')->get_question_info_by_id($_GET['question_id']);

			$email_hash = base64_encode(H::encode_hash(array(
					'email' => $_POST['email']
			)));
			
			$this->model('email')->action_email(email_class::TYPE_INVITE_QUESTION, $email, get_js_url('/question/' . $_GET['question_id'] . '?fromuid-' . $this->user_id . '__fromemail-' . $email_hash), array(
				'user_name' => $this->user_info['user_name'], 
				'question_title' => $q_info['question_content']
			));
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'question_id' => $_GET['question_id']
			), 1, '邀请成功。'));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '发送邀请失败。'));
		}
	}

	function get_invite_email_list_action()
	{
		$invite_email = $this->model('question')->get_invite_email($_GET['question_id'], $this->user_id, '5');
		
		TPL::assign('list', $invite_email);
		
		TPL::output('question/ajax/invites');
	}

	function remove_comment()
	{
		if (! in_array($_GET['type'], array(
			'answer', 
			'question'
		)))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '错误的请求。'));
		}
		
		if (! $_GET['comment_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '评论ID错误。'));
		}
		
		$comment = $this->model($_GET['type'])->get_comment_by_id($_GET['comment_id']);
		
		if (! $this->user_info['permission']['is_moderator'] && ! $this->user_info['permission']['is_administortar'] && $this->user_id != $comment['uid'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '你没有权限删除该评论。'));
		}
		
		if ($this->model($_GET['type'])->remove_comment($_GET['comment_id']))
		{
			if ($_GET['type'] == 'answer')
			{
				$this->model('answer')->update_answer_comments_count($comment['answer_id']);
			}
			else if ($_GET['type'] == 'question')
			{
				$this->model('question')->update_question_comments_count($comment['question_id']);
			}
			
			H::ajax_json_output(AWS_APP::RSM(null, 1, '删除评论成功。'));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '删除评论失败。'));
		}
	}

	function load_attach_action()
	{
		if ($attach = $this->model('publish')->get_attach_by_id($_GET['id']))
		{
			TPL::assign('attach', $attach);
			
			echo 'document.write("' . str_replace(array(
				"\r", 
				"\n", 
				"\t"
			), '', addslashes(TPL::output('question/ajax/load_attach', false))) . '");';
		}
	}
	
	function answer_force_fold()
	{
		if (! $this->user_info['permission']['is_moderator'] && ! $this->user_info['permission']['is_administortar'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "你所在的用户组没有权限强制折叠回复。"));
		}
		
		$answer_info = $this->model('answer')->get_answer_info_by_id(intval($_POST['answer_id']));
		
		if(!$answer_info)
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "回复不存在。"));
		}
		
		if(!$answer_info['force_fold'])
		{
			$this->model('answer')->update_answer_by_id(intval($_POST['answer_id']), array('force_fold' => 1));
			
			H::ajax_json_output(AWS_APP::RSM(array('action' => 'fold'), 1, "强制折叠回复。"));
		}
		else
		{
			$this->model('answer')->update_answer_by_id(intval($_POST['answer_id']), array('force_fold' => 0));
			
			H::ajax_json_output(AWS_APP::RSM(array('action' => 'unfold'), 1, "撤销折叠回复。"));
		}
	}
}